package com.cqc.networkmanager01;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * Created by ${cqc} on 2017/5/12.
 */

public class ConnectChangeReceiver extends BroadcastReceiver {

    private static final String TAG = "ConnectChangeReceiver";

    /**
     * 获取ConnectivityManager
     * 获取ConnectionInfo
     * 判断网络是否可用
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
                Log.d(TAG, "联网正常");
            } else {
                Log.d(TAG, "联网失败");
            }
        }
    }
}
